/*******************************************************************************
 * *
 * *             COPYRIGHT 2012 CLEARLEAP, INC. ALL RIGHTS RESERVED.
 * *             CLEARLEAP CONFIDENTIAL PROPRIETARY
 * *             TEMPLATE VERSION R01.00
 * *
 * ********************************************************************************
 * *
 * *  FILE NAME: KeyStoreInfo.java
 * *
 * *---------------------------------- PURPOSE ------------------------------------
 * *
 * * Key management utilities
 * *
 * *
 * *----------------------------- REVISION HISTORY --------------------------------
 * *
 * * Date      ID        Tracking#      Description
 * * --------  --------  -------------  -------------------------------------------
 * * 01.07                              Created
 * *
 * *--------------------------- INCLUDES ----------------------------*
 * *
 ******************************************************************************/
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easysale.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;

/**
 *
 * @author minhtran
 */
public class KeyStoreInfo {
    /**
     *
     * @param store
     * @param sPass
     * @param kPass
     * @param alias
     * @return
     * @throws CertificateException
     * @throws IOException
     * @throws UnrecoverableKeyException
     * @throws KeyStoreException
     * @throws NoSuchAlgorithmException
     */
    public static KeyPair getKeyPair(String store, String sPass, String kPass,
            String alias) throws CertificateException, IOException,
            UnrecoverableKeyException, KeyStoreException,
            NoSuchAlgorithmException {
        KeyPair result;
        KeyStore ks = loadKeyStore(store, sPass);
        Key key = null;
        PublicKey publicKey = null;
        PrivateKey privateKey = null;
        if (ks.containsAlias(alias)) {
            key = ks.getKey(alias, kPass.toCharArray());
            if (key instanceof PrivateKey) {
                Certificate cert = ks.getCertificate(alias);
                publicKey = cert.getPublicKey();
                privateKey = (PrivateKey) key;
                result = new KeyPair(publicKey, privateKey);
            } else {
                result = null;
            }
        } else {
            result = null;
        }
        return result;
    }

    public static PublicKey getPublicKey(String store, String sPass,
            String alias) throws KeyStoreException, NoSuchAlgorithmException,
            CertificateException, IOException {
        KeyStore ks = loadKeyStore(store, sPass);
        Certificate cert = ks.getCertificate(alias);
        return cert.getPublicKey();
    }

    private static KeyStore loadKeyStore(String store, String sPass)
            throws KeyStoreException, NoSuchAlgorithmException,
            CertificateException, IOException {
        KeyStore myKS = KeyStore.getInstance("JKS");
        FileInputStream fis = new FileInputStream(store);
        myKS.load(fis, sPass.toCharArray());
        fis.close();
        return myKS;
    }
}
